<?php

class ProfileController extends BaseController {

	/*
	|--------------------------------------------------------------------------
	| Default prfoile Controller
	|--------------------------------------------------------------------------
	|

	|
	*/


    /**
     * The layout that should be used for responses.
     */
    protected $layout = 'layouts.master';

	const GLOBAL_MESSAGE = 'global';
    /**
	* Get specific user profile by user id	
    */
	public function profile($user_id)
	{
		
      
		if($user_id!=null){

				$user = User::where('id','=',$user_id)->first();

				$profile = User::find($user_id)->profile;

                if(!$user){
                	 return Redirect::route('home')
                        ->with(self::GLOBAL_MESSAGE,"User can not find");
                } 

				$profile = User::find($user_id)->profile;
  
				 if(!$profile){
                	 return Redirect::route('home')
                        ->with(self::GLOBAL_MESSAGE,"profile can not find");
                } 
			    $data['user'] = $user;

				$data['profile'] =  $profile ;

                $networkname  =  User::getNetWorkName();;

				$data['newtork'] =   $networkname  ;
			

				foreach( User::getAllNetWorkName() as $netWork) {
 					   $network[$netWork->network] = $netWork->name;
				}	
				$data['networknames']  = $network;

				if($profile->company_id != 0 )	{

                $company  =  DB::table('company')->where('id','=', $profile->company_id)->first() ;

				$data['companyname'] =   $company->name ;

					

				
				


				}
					if($user->count()){
					        $user  = $user->first();

					        if($user_id==Auth::getUser()->id){ 	

									return View::make('profile.editprofile', $data);
							}
							else
							{
									return View::make('profile.profile', $data);		
							}

				     
							return App::abort(404);
				}

	}	
	}

public function company($company_id)
 {
       if($company_id!=null){
       

 			$company = DB::table('company')->where('id', $company_id)->first();
       
        
            if($company){



 			$data['company'] = $company ;

 			$pics = explode(";", $company->pics);		

			$data['pics'] = $pics ;

                    return View::make('profile.company',$data);
	        }
         
        	return App::abort(404);
    }

    }

public function postEditprofile(){
	
		
		  $input  =  array(
          'email'           => Input::get('email'),
          'username'        => Input::get('username'),
      	   );
  
    		$validator =  array(
          'email'           =>'email|required',
          'username'        => 'required',
        );
   

 		$validator = Validator::make($input,$validator);

    	if($validator->fails() ){
        	return Redirect::route('profile',Auth::user()->id)->withErrors($validator)->withInput();
           
      }else{

		$user = User::find(Auth::user()->id);
		$user->username = Input::get('username');
		$user->email 	 = Input::get('email');
		$user->save();
		
		$profile = Profile::find(Auth::user()->id);

			$profile->first_name	= Input::get('firstname');
			$profile->last_name		= Input::get('lastname');
			$profile->addres1		= Input::get('addr1');
			$profile->addres2		= Input::get('addr2');
			$profile->city   		= Input::get('city');
			$profile->country		= Input::get('country');
			$profile->province		= Input::get('Province');
			$profile->network_id	= Input::get('network');
		
			$profile->save();
	        return Redirect::route('profile',Auth::user()->id)->with(self::GLOBAL_MESSAGE,trans('messages.yourprofileupdated'));
	}

}

	public function create()
    {
    	return View::make('profile.addcompany');
    }

	
	public function postCreateCompany()
    {

          $name = Input::get('name');
          $desc = Input::get('desc');

    	 $input  =  array(
          'name'           => $name,
          );
  
    		$validator =  array(
          'name'           =>'required',
	       );
   
   		$validator = Validator::make($input,$validator);

    	if($validator->fails() ){
        	return Redirect::route('company/create')->withErrors($validator)->withInput();
           
      }else{

        $company = Company::create(array('name'=> $name ,'content'=> $desc) );

        $company->save();

        $insertedId = $company->id;

		$user = User::find(Auth::user()->id);
		$user->username = Input::get('username');
		$user->email 	 = Input::get('email');
		$user->save();
		
		$profile = Profile::find(Auth::user()->id);

			$profile->comapny_id 	= $insertedId ;
			$profile->save();
	        return Redirect::route('profile',Auth::user()->id)->with(self::GLOBAL_MESSAGE,trans('messages.yourprofileupdated'));
	}
    }    
}